package yl.hs.bmipfull.service.laboratorydata.imple;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabExperimentItemsDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmInspectionReportDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmSandReportItemDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmSandReportScreeningDao;
import yl.hs.bmipfull.pojo.laboratorydata.*;
import yl.hs.bmipfull.service.laboratorydata.ILabRmSandReportItemService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴高丰
 * @apiNote 服务层接口实现
 * @since 2022年06月22日 16时13分50秒 <br/>
 */
@Service
public class LabRmSandReportItemServiceImpl extends LaboratorydataBaseService implements ILabRmSandReportItemService {

    @Autowired
    ILabRmSandReportItemDao dao;
    @Autowired
    ILabExperimentItemsDao itemdao;
    @Autowired
    ILabRmSandReportScreeningDao screeningdao;
    @Autowired
    ILabRmInspectionReportDao reportdao;
    @Override
    public List<LabRmSandReportItemEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabExperimentItemsEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabRmSandReportItemEntity.class);
        return dao.queryPage(pageQuery.getQuery(),null);
    }

    @Override
    public List<LabRmSandReportItemEntity> getByReportSN(String reportSN) {
        return dao.getByReportSN(reportSN);
    }

    @Override
    public PageResult<LabRmSandReportItemEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabRmSandReportItemEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabRmSandReportItemEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),LabRmSandReportItemEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public Map<String,Object> findBySn(String sn) {
        Map<String,Object> map = new HashMap<>();
        val labRmSandReportScreeningEntities = screeningdao.queryBySN(sn);
        val bySN = reportdao.findBySN(sn);
        val bySN1 = dao.findBySN(sn);
        map.put("LabRmInspectionReportEntity",bySN);
        map.put("LabRmSandReportItemEntity",bySN1);
        map.put("LabRmSandReportScreeningEntity",labRmSandReportScreeningEntities);
        return  map;
    }

    @Override
    public Integer saveLabRmSandReportItem(Map<String, Object> model, String sn) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        Integer result = 0;
        LabRmInspectionReportEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabRmInspectionReportEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabRmInspectionReportEntity.class));
            result = reportdao.insertNew(entity);
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmSandReportItemEntity.class);
            subDataEntity.setReportSN(entity.getSn());
            result =dao.insertNew(subDataEntity);
            if (result > 0) {
                writeLog(ActionType.CREATE, subDataEntity.getReportSN(), "插入信息", Tl.toJson(subDataEntity), LabRmSandReportItemEntity.class);
            }
        } else {
            //执行修改
            entity = reportdao.findBySN(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = reportdao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新信息", Tl.toJson(entity), LabRmInspectionReportEntity.class);
            }
            dao.deleteByReportSN(entity.getSn());
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmSandReportItemEntity.class);
            subDataEntity.setReportSN(entity.getSn());
            result =dao.insertNew(subDataEntity);
            if (result > 0) {
                writeLog(ActionType.CREATE, subDataEntity.getReportSN(), "插入信息", Tl.toJson(subDataEntity), LabRmSandReportItemEntity.class);
            }


        }
        return result;
    }

    @Override
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabRmSandReportItemEntity.class);
        }
        return result;
    }
}